<template>
  <div class="app-container">
    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input clearable v-model="searchForm.keyword" placeholder="搜索"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="#" width="95">
        <template v-slot:default="{scope,row}">
          {{ row.id }}
        </template>
      </el-table-column>
      <el-table-column label="提交人" width="180" align="center">
        <template v-slot:default="{scope,row}">
          <span>{{ row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" width="180" align="center">
        <template v-slot:default="{scope,row}">
          <span>{{ row.type }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="100" align="center">
        <template v-slot:default="{scope,row}">
          <el-tag :type="row.status=='1'?'success':'danger'">{{ row.status_text }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图片" width="100">
        <template v-slot:default="{scope,row}">
          <el-image
            v-if="row.images.length > 0"
            style="width: 50px; height: 50px"
            :src="row.images[0]"
            :preview-src-list="row.images"
          >
          </el-image>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" width="500">
        <template v-slot:default="{scope,row}">
          {{ row.content }}
        </template>
      </el-table-column>


      <el-table-column label="提交时间" width="180" align="center">
        <template v-slot:default="{scope,row}">
          <span>{{ row.created_at }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="180" align="center">
        <template v-slot:default="{scope,row}">
          <span>{{ row.updated_at }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="created_at" label="操作" width="150" fixed="right">
        <template v-slot:default="{scope,row}">
          <el-row>
            <el-button v-if="row.status==0" title="标记为已处理" size="mini" type="primary" icon="el-icon-view" circle
                       @click="save(row.id)"
            ></el-button>
            <el-button v-if="row.status==0" size="mini" type="info" icon="el-icon-delete" circle @click="del(row.id)"
            ></el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="fetchData"
    />


    <el-dialog title="回复反馈并标记为已解决" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="回复内容" :label-width="200">
          <el-input v-model="form.reply" type="textarea" placeholder="请输入回复内容" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save('ok')">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import api from '@/api/feedback'
import Pagination from '@/components/Pagination'

export default {
  components: {
    Pagination
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '1': 'success',
        '0': 'gray',
        '-1': 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      form: {
        id: 0,
        reply: ''
      },
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        sort: ''
      },
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleCategorySelect(category) {
      this.searchForm.category_id = category
    },
    fetchData() {
      this.listLoading = true
      api.getList(this.listQuery).then(response => {

        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    },
    del(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.del({ id: id }).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    save(id) {
      if (id !== 'ok') {
        this.form.id = id
        if (this.form.id != id) {
          this.form.reply = ''
        }
        this.dialogFormVisible = true
        return
      }
      this.dialogFormVisible = false

      this.$confirm('确定标记为已处理吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.update({ id: this.form.id, status: 1, type: 'setStatus', reply: this.form.reply }).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      }).finally(()=>{
        this.form.id = 0
        this.form.reply = ''
      })
    }
  }

}
</script>
